package sevletpojo;

import server.HttpProtocoUtil;
import server.StaticUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 王瑞
 * @description:
 * @date 2020/3/1  18:46
 */
public class Response {

    private OutputStream outputStream;

    public Response(OutputStream outputStream){
        this.outputStream =outputStream;
    }

    public void writeByOutPut(String content) throws IOException {
        this.outputStream.write(content.getBytes());
    }


    public void outPutHtml(String path) throws IOException {


        //获取该路径的资源文件路径（磁盘上的绝对路径）
        String absPath = StaticUtil.getAbsolutePath(path);

        File file = new File(absPath);
        if (file.exists()&&file.isFile()){
            StaticUtil.writeStaticResource(new FileInputStream(file),outputStream);
        }else {
            writeByOutPut(HttpProtocoUtil.getHttpHeader404());
        }


    }

}
